const express = require('express')
const app = express()
const port = 7878
const router = require('./router/index')
const ai_router = require('./router/ai')
const scan_login = require('./router/scan-login')
const cors = require('cors')
const bodyParser = require('body-parser')
const { makePattern, ServerName_LogText, HttpServer_LogText, SocketServer_LogText } = require('./utils/console_log')
require('./mongodb/index')

console.log(makePattern(ServerName_LogText, 'blue'))

// 跨域处理
app.use(cors())
// post请求 表单数据
app.use(express.urlencoded({ extended: true }))
app.use(bodyParser.json())
// 静态资源共享
app.use(express.static('upload/user'))
app.use(express.static('upload/group'))
app.use(express.static('upload/space'))
app.use(express.static('upload/private_chat'))
app.use(express.static('upload/group_chat'))
app.use(express.static('public/img'))
app.use(express.static('public/video'))

app.use('/', router)
app.use('/ai', ai_router)
app.use('/scan', scan_login)

// http服务器
app.listen(port, () => {
    console.log(makePattern(HttpServer_LogText, 'green'))
    console.log(`🌍server running at http://127.0.0.1:${port}`)
})

/**======================================== socket.io ==================================== */
// 创建socket服务器
const http = require('http')
const socketIO = require('socket.io')
const server = http.createServer(app)
const socket_port = 24000
 
const io = socketIO(server,{
    cors: {
        origin: '*'
    }
})
require('./socket/index')(io)

server.listen(socket_port, () => {
    console.log(makePattern(SocketServer_LogText, 'green'))
    console.log(`🛰️ socket_server running at http://127.0.0.1:${socket_port}`)
})